package com.company.HDFS;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * @ClassName: HbaseToHdfs
 * @Description: hbase全表数据导入hdfs
 * @author gy
 * @date 2018年7月13日 下午3:59:11
 *
 */
public class HbaseToHdfs {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        String tablename = "mr:demo";// 表名字
        String outputpath = "hdfs://node1/output";//输出路径
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "zookeeper 192.168.88.100");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("zookeeper.znode.parent", "/hbase");

        Job job = Job.getInstance(conf, "HbaseToHdfs");
        job.setJarByClass(HbaseToHdfs.class);
        Scan scan = new Scan();
        TableMapReduceUtil.initTableMapperJob(tablename, scan, doMapper.class, Text.class, Text.class, job);
        job.setReducerClass(WordCountHbaseReaderReduce.class);
        FileOutputFormat.setOutputPath(job, new Path(outputpath));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

    public static class doMapper extends TableMapper<Text, Text> {
        protected void map(ImmutableBytesWritable key, Result value, Context context)
                throws IOException, InterruptedException {
            // 获取对应的字段
            String TOWNSHIP = new String(value.getValue("ColumnFamily1".getBytes(), "TOWNSHIP".getBytes()), "utf-8");
            
            context.write(new Text(""), new Text(TOWNSHIP));
        }
    }
    
    public static class WordCountHbaseReaderReduce extends Reducer<Text, Text, Text, Text> {
        private Text result = new Text();

        protected void reduce(Text key, Iterable<Text> values, Context context)
                throws IOException, InterruptedException {
            for (Text val : values) {
                result.set(val);
                context.write(key, result);
            }
        }
    }
}